﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module;
using System.Web.UI.HtmlControls;
using BLL.Common;

namespace App
{
    public partial class NewsList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                pageInit();
            }
        }

        protected void rptNewsList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                NewsModel p = e.Item.DataItem as NewsModel;
                HtmlAnchor link = e.Item.FindControl("lb_news") as HtmlAnchor;

                if (p != null && p.NewsId > 0 && link != null)
                {
                    link.InnerText = p.NewsTitle + " (" + p.NewsPubDate.ToShortDateString() + ")";
                    link.HRef = p.NewsUrl;
                    link.Title = p.NewsDescription;
                }

            }
            if (e.Item.ItemType == ListItemType.Footer)
            {
                if (rptNewsList.Items.Count <= 0)
                {
                    ((Label)e.Item.FindControl("lblNoRecord")).Visible = true;
                }
            }
        }

        protected void anpOrders_PageChanged(object sender, EventArgs e)
        {
            pageInit();
        }

        private void pageInit()
        {
            int newsType = Contexts.GetNewsType();
            List<NewsModel> Pages = getPages(newsType);
            if (Pages != null)
            {

                this.rptNewsList.DataSource = Pages;
                this.rptNewsList.DataBind();
            }
        }

        private List<NewsModel> getPages(int newsType)
        {
            List<NewsModel> newsList = null;

            switch (newsType)
            {
                case 0:
                    this.lbl_NewsCategory.Text = "全部新闻";
                    break;
                case 1:
                    this.lbl_NewsCategory.Text = "体育新闻";
                    break;
                case 2:
                    this.lbl_NewsCategory.Text = "社会新闻";
                    break;
                case 3:
                    this.lbl_NewsCategory.Text = "财经新闻";
                    break;
                case 4:
                    this.lbl_NewsCategory.Text = "焦点新闻";
                    break;
                case 5:
                    this.lbl_NewsCategory.Text = "IT新闻";
                    break;
                case 6:
                    this.lbl_NewsCategory.Text = "娱乐新闻";
                    break;
            }
            //s(newsType)
            //    {
            //        case 0:
            //          this.lbl_NewsCategory.Text="全部新闻";
            //        break;

            //    }
            if (newsType > 0)
            {
                newsList = BLL.BNews.getInstance().GetAllNewsByNewsType(newsType);

            }
            else
            {
                newsList = BLL.BNews.getInstance().GetAllNews();
            }

            if (newsList != null && newsList.Count > 0)
            {
                this.anpOrders.RecordCount = newsList.Count;
                List<NewsModel> tempPages = new List<NewsModel>();
                tempPages.AddRange(newsList);
                int currentNum = 0;

                //分页数据处理
                foreach (NewsModel info in tempPages)
                {
                    currentNum++;
                    if (currentNum < anpOrders.PageSize * (anpOrders.CurrentPageIndex - 1) + 1 || currentNum > anpOrders.PageSize * anpOrders.CurrentPageIndex)
                    {
                        newsList.Remove(info);
                    }
                }
            }

            return newsList;
        }

    }
}